<?php

namespace App\Repositories;


use App\Interfaces\StockService;
use App\Models\Goods;
use App\Models\GoodsStockHistory;
use Config;
use App\Http\Controllers\Admin\GoodsStockHistoryController;
use Auth;
use App\Interfaces\GoodsStockHistoryService;
use App\Exceptions\Interfaces\StockServiceException;
use DB;

class StockRepository implements StockService{


    /**
     * 库存操作
     * @param $goods_id            商品id
     * @param $user_id             用户id
     * @param $goods_stock         库存数量
     * @param $operation           库存操作，plus:加库存；reduce:减库存
     * @param $describe            操作描述
     * @param string $is_all_goods_exception    //是否抛出所有商品的异常信息，0：不抛出所有的商品异常，只抛出第一个商品的第一个异常；1：抛出所有的商品异常
     * @param $goodsStockHistoryService            库存操作日志记录服务
     * @return mixed
     */
    public function updateStock($goods_id, $user_id, $goods_stock, $operation, $describe, GoodsStockHistoryService $goodsStockHistoryService, $is_all_goods_exception = 0){

        $stock = Goods::where('goods_id', $goods_id)->pluck('goods_number');
        $number = $goods_stock;

        DB::beginTransaction();//开启事务,开始批量操作
        try {

            if($operation=='plus'){

                $goods_stock = $stock + $goods_stock;
                $operation = 1;

            }elseif($operation=='reduce'){

                if($goods_stock>$stock && !$is_all_goods_exception){
                    throw new StockServiceException('当前库存不足');
                }//end if()

                $goods_stock = $stock - $goods_stock;
                $operation = 2;
            }else{

                $operation = 3;
            }//end if()

            //GoodsStockHistory::create(['goods_sn' => $goods_sn, 'user_id' => $user_id, 'describe' => $describe, 'stock_status' => $operation, 'add_time' => time()]);
            $goodsStockHistoryService->updateStockHistory($goods_id, $user_id, $describe, $number, $operation);

            $goodsStock = Goods::where('goods_id', $goods_id);

            $goodsStock = $goodsStock->update(['goods_number'=>$goods_stock]);


        }catch (StockServiceException $ex ){
            DB::rollback();

            throw new StockServiceException($ex->getMessage());
        }

        DB::commit();

        return true;

    }//end func updateStock



    /**
     * 批量库存操作
     * @param  [type] $goods                    [商品数组]
     * @param  [type] $user_id                  [操作的用户id]
     * @param  [type] $goodsStockHistoryService [商品库存操作记录服务]
     * @param  [type] $solrService              [solr服务]
     * @return [type]                           [description]
     */
    public function batchUpdateStock($goods, $user_id, $goodsStockHistoryService, $solrService)
    {

        if (count($goods)<1) {

            throw new StockServiceException('要批量操作的不是商品数组');
        }

        foreach ($goods as $key => $good) {

            $is_err = 0;        //是否有误，0：无误，1：有误；
            $stock = Goods::where('goods_id', $good['goods_id'])->pluck('goods_number');
            $number = $good['storage_num'];

            DB::beginTransaction();//开启事务,开始批量操作
            try {

                switch ($good['operation']) {

                    case '1':

                        $goods_stock = $stock + $number;

                        break;

                    case '2':

                        if($number>$stock){

                            $is_err = 1;        //是否有误，0：无误，1：有误；
                            // throw new StockServiceException('当前库存不足');
                        }//end if()

                        $goods_stock = $stock - $number;

                        break;
                }

                $goodsStockHistoryService->updateStockHistory($good['goods_id'], $user_id, $good['describe'], $number, $good['operation'], 1, $is_err);

                if ($is_err) {
                    continue;
                }

                $goodsStock = Goods::where('goods_id', $good['goods_id']);

                $goodsStock = $goodsStock->update(['goods_number'=>$goods_stock]);

                //同步solr
                $goodsObj = Goods::find($good['goods_id']);

                $goodsObj->stock=$goods_stock;

                $solrService->update($goodsObj);

            }catch (StockServiceException $ex ){
                DB::rollback();

                // throw new StockServiceException($ex->getMessage());
            }

            DB::commit();
        }

        return true;
    }


}//end class GoodsRepository